package com.example.bigyu.test02;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by bigyu on 2016/9/13.
 */
public class TestView extends View {
    private List<Line> lines;
    private Paint paint;
    private String[] time = new String[]{"07:00", "08.00", "09:00", "10:00", "11:00", "12:00", "中午", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "晚上", "19:00", "20:00", "21:00", "22:00"};


    private float screenWidth;
    private float screenHeight;
    private int timeHeight;


    private Canvas canvas;
    private Line line;
    private LineCompare lineCompare;

    private Rect timeRect;

    private List<TimeTxt> timeTxts;

    public TestView(Context context) {
        super(context);
        initData(context);
    }

    private void initData(Context context) {
        lines = new ArrayList<>();
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(20);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(40);
        lineCompare = new LineCompare();
        timeRect = new Rect();

        paint.getTextBounds("07:00", 0, "07:00".length(), timeRect);
        timeHeight = timeRect.height();
        Log.e("vvvvvvvv", "initData: " + timeHeight + "     " + timeRect.width());
    }


    // 设置7:00 -- 22:00 的时间
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        screenHeight = getMeasuredHeight();
        screenWidth = getMeasuredWidth();

        timeTxts = new ArrayList<>();
        //竖直方向时间每一项的高度
        int singHeight = (int) (screenHeight / (time.length + 3));
        blackHeight = singHeight - timeHeight;
        //设置时间
        for (int i = 0; i < time.length; i++) {
            int vHeight = singHeight + singHeight * (i + 1);
            timeTxts.add(new TimeTxt(time[i], 100, vHeight, Color.BLACK));
        }
    }

    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData(context);
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData(context);
    }

    private boolean isClear;

    public void setClear(boolean clear) {
        isClear = clear;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
// 清除画布上的线
        if (isClear) {
            lines.clear();
            isClear = false;
        }
        // 设置横线的颜色
        paint.setColor(Color.BLACK);
        // 设置画笔的宽度
        paint.setStrokeWidth(1);
        // 画每一个横线
        for (int i = 0; i < timeTxts.size(); i++) {
            // 设置7:00 -- 22:00 的时间
            TimeTxt timeTxt = timeTxts.get(i);
            paint.setColor(timeTxt.getColor());
            String timeTxtTimeTxt = timeTxt.getTimeTxt();
            if (i == 6 || i == 13) {
                timeTxtTimeTxt = "";
            }
            canvas.drawText(timeTxtTimeTxt, timeTxt.getStartX(), timeTxt.getStartY(), paint);
            //开始的X坐标
            float v = (float) (timeTxt.getStartX() + timeRect.width());
            // 每一个横线的Y坐标
            float y = timeTxt.getStartY() - timeHeight / 2;
            canvas.drawLine(v, y, screenWidth - timeRect.width(), y, paint);
        }
        // 画任务时间 ， 画园
        paint.setStrokeWidth(40);
        for (int i = 0; i < lines.size(); i++) {
            paint.setColor(Color.BLUE);
            canvas.drawLine((float) (timeTxts.get(0).getStartX() + timeRect.width() * 4), lines.get(i).getStartY(), (float) (timeTxts.get(0).getStartX() + timeRect.width() * 4), lines.get(i).getEndY(), paint);
            float v = (float) (timeTxts.get(i).getStartX() + timeRect.width());
            canvas.drawCircle((float) (timeTxts.get(0).getStartX() + timeRect.width() * 4), lines.get(i).getStartY(), 20, paint);
            canvas.drawCircle((float) (timeTxts.get(0).getStartX() + timeRect.width() * 4), lines.get(i).getEndY(), 20, paint);
            paint.setColor(Color.WHITE);
            canvas.drawCircle((float) (timeTxts.get(0).getStartX() + timeRect.width() * 4), lines.get(i).getStartY(), 10, paint);
            canvas.drawCircle((float) (timeTxts.get(0).getStartX() + timeRect.width() * 4), lines.get(i).getEndY(), 10, paint);
        }


    }


    float startY;
    float endY;

    private float blackHeight;

    private boolean isLining;

    //圆点吸附
    public float check(float y, boolean isStart, Line l) {
        for (int i = 0; i < timeTxts.size(); i++) {
            float v = timeTxts.get(i).getStartY();
            if (y < timeTxts.get(0).getStartY()) {
                setLineText(isStart, l, 0);
//                Log.e("vvvvvvvvvvvvvvvvvvv", "check: " + (timeTxts.get(0).getStartY() - timeHeight / 2));
                return timeTxts.get(0).getStartY() - timeHeight / 2;
            }
            if (y > timeTxts.get(timeTxts.size() - 1).getStartY()) {
                setLineText(isStart, l, timeTxts.size() - 1);
//                Log.e("vvvvvvvvvvvvvvvvvvv", "check: " + (timeTxts.get(timeTxts.size() - 1).getStartY() - timeHeight / 2));
                return timeTxts.get(timeTxts.size() - 1).getStartY() - timeHeight / 2;
            }
            if (y < v + blackHeight / 2 && y > v - timeHeight - blackHeight / 2) {
//                Log.e("vvvvvvvvvvvvvvvvvvv", "check: " + (v - timeHeight / 2));
                setLineText(isStart, l, i);
                return v - timeHeight / 2;
            }

        }
        return 0;
    }

    /**
     * 设置线的时间文字
     *
     * @param isStart
     * @param l
     * @param i
     */
    private void setLineText(boolean isStart, Line l, int i) {
        if (isStart) {
            l.setStartTxt(timeTxts.get(i).getTimeTxt());
        } else {
            l.setEndTxt(timeTxts.get(i).getTimeTxt());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //判斷集合中是否存在这条线
                isInList(event);
                //如果不存在创建一条
                if (!isLining) {
                    this.line = new Line();
                    lines.add(line);
                    startY = check(event.getY(), true, line);
                    endY = check(event.getY(), false, line);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                endY = event.getY();
                line.setStartY(startY);
                line.setEndY(endY);
                invalidate();
                break;

            case MotionEvent.ACTION_UP:
                endY = check(event.getY(), false, line);
                line.setStartY(startY);
                line.setEndY(endY);
                float tmp;
                // XY换位置
                changXY();
                //排序
                Collections.sort(lines, lineCompare);
                //移除重合的线
                removeSome();
                //设置所有线的开始时间和结束时间
                setLineStartEndText();
                //重置时间表的颜色
                resetTimeColor();
                //设置时间表的颜色
                setTimeColor();
                invalidate();
                isLining = false;
                break;
        }
        return true;
    }

    //设置时间表的颜色
    private void setTimeColor() {
        for (int i = 0; i < lines.size(); i++) {
            Line line1 = lines.get(i);
            Log.e("vvvvvvvvvvvvvvvv", line1.getStartTxt() + "   " + line1.getEndTxt());
            boolean isStartColor = false, isEndColor = false;
            for (int j = 0; j < timeTxts.size(); j++) {
                TimeTxt timeTxt = timeTxts.get(j);
                Log.e("vvvvvvvvvvvvvvvv", timeTxt.getTimeTxt());
                if (!isStartColor && line1.getStartTxt().trim().equals(timeTxt.getTimeTxt())) {
                    timeTxt.setColor(Color.BLUE);
                    isStartColor = true;
                }
                if (!isEndColor && line1.getEndTxt().trim().equals(timeTxt.getTimeTxt())) {
                    timeTxt.setColor(Color.BLUE);
                    isEndColor = true;
                }
            }
        }
    }

    private void resetTimeColor() {
        for (int i = 0; i < timeTxts.size(); i++) {
            TimeTxt timeTxt = timeTxts.get(i);
            timeTxt.setColor(Color.BLACK);
        }
    }

    //设置所有线的开始时间和结束时间
    private void setLineStartEndText() {
        for (int i = 0; i < lines.size(); i++) {
            Line line1 = lines.get(i);
            check(line1.getStartY(), true, line1);
            check(line1.getEndY(), false, line1);
        }
    }

    //移除重合的线
    private void removeSome() {
        for (int i = 0; i < lines.size(); i++) {
            Line end = lines.get(i);
            for (int j = i + 1; j < lines.size(); j++) {
                Line start = lines.get(j);
                if (end.getEndY() >= start.getStartY()) {
                    if (!(end.getEndY() > start.getEndY())) {
                        end.setEndY(start.getEndY());
                    }
                    lines.remove(j);
                    j--;
                }
            }
        }
    }

    // XY换位置
    private void changXY() {
        float tmp;
        if (line.getStartY() > line.getEndY()) {
            tmp = line.getStartY();
            line.setStartY(line.getEndY());
            line.setEndY(tmp);
        } else if (line.getStartY() == line.getEndY()) {
            lines.remove(lines.size() - 1);
        }
    }

    //判斷集合中是否存在这条线
    private void isInList(MotionEvent event) {
        for (int i = 0; i < lines.size(); i++) {
            Line eLine = lines.get(i);
            if (event.getY() >= eLine.getStartY() && event.getY() <= eLine.getEndY()) {
                this.line = eLine;
                if (event.getY() > (eLine.getEndY() + eLine.getStartY()) / 2) {
                    startY = eLine.getStartY();
                    endY = startY;
                } else {
                    startY = eLine.getEndY();
                    endY = event.getY();
                }
                isLining = true;
                break;
            }
        }
    }


    public List<Line> getTime() {
        return lines;
    }

    //比较器
    class LineCompare implements Comparator<Line> {
        @Override
        public int compare(Line line, Line t1) {
            return line.getStartY() - t1.getStartY() >= 0 ? 1 : -1;
        }
    }
}
